package cn.b.sky.mes.cm.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.cm.model.WCStandardCapacity;
import cn.b.sky.mes.cm.model.WorkCenter;
import cn.b.sky.mes.cm.model.condition.WCStandardCapacityCondition;
import cn.b.sky.mes.cm.service.WCStandardCapacityService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : WCStandardCapacity.java<br></strong>
 * <strong>Package : cn.b.sky.mes.cm.controller<br></strong>
 * <strong>Description : </strong>工作中心标准产能控制器<br> 
 * <strong>Create on : 2016年1月13日 上午11:50:59<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("wcStandardCapacity")
public class WCStandardCapacityController extends SecurityController {

  @Resource
  WCStandardCapacityService wcStandardCapacityService;

  @RequestMapping("wcStandardCapacity_list")
  public String selectWCStandardCapacityListByCondition(
      @ModelAttribute("condition") WCStandardCapacityCondition wcStandardCapacityCondition, Model model,
      String navTabId) {
    wcStandardCapacityCondition.setSiteCode(getSiteCode());
    List<WCStandardCapacity> list = wcStandardCapacityService
        .selectWCStandardCapacityListByCondition(wcStandardCapacityCondition);
    int count = wcStandardCapacityService.selectWCStandardCapacityCountByCondition(wcStandardCapacityCondition);
    wcStandardCapacityCondition.setData(list);
    wcStandardCapacityCondition.setTotalCount(count);
    model.addAttribute("page", wcStandardCapacityCondition);
    model.addAttribute(NAVTABID, navTabId);
    return "cm/wcStandardCapacity/wcStandardCapacity_list";
  }

  @RequestMapping("addWCStandardCapacity")
  public String addWCStandardCapacity(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    return "cm/wcStandardCapacity/wcStandardCapacity_add";
  }

  @RequestMapping("editWCStandardCapacity")
  public String editWCStandardCapacity(Model model, String navTabId, String id) {
    WCStandardCapacity wcStandardCapacity = wcStandardCapacityService.getWCStandardCapacity(id, getSiteCode());
    model.addAttribute("wcStandardCapacity", wcStandardCapacity);
    model.addAttribute(NAVTABID, navTabId);
    return "cm/wcStandardCapacity/wcStandardCapacity_add";
  }

  @RequestMapping("viewWCStandardCapacity")
  public String viewWCStandardCapacity(Model model, String navTabId, String id) {
    WCStandardCapacity wcStandardCapacity = wcStandardCapacityService.getWCStandardCapacity(id, getSiteCode());
    model.addAttribute("wcStandardCapacity", wcStandardCapacity);
    model.addAttribute(NAVTABID, navTabId);
    return "cm/wcStandardCapacity/wcStandardCapacity_view";
  }

  @RequestMapping(value = "saveWCStandardCapacity")
  @ResponseBody
  public DWZResult saveWCStandardCapacity(Model model, WCStandardCapacity wcStandardCapacity, String navTabId,
      String id) {
    if (wcStandardCapacity == null) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    wcStandardCapacity.setSiteCode(getSiteCode());
    setCreateInfo(wcStandardCapacity);
    if (StringUtils.isBlank(id)) {
      WorkCenter workCenter = wcStandardCapacityService.getUnusedWorkCenter(wcStandardCapacity.getWorkCenterCode(),
          getSiteCode());
      if (workCenter == null || workCenter.getLockFlag() == 1) {
        return ajaxError(getImeViewMessageI18n(null, "workCenterNotExitstOrIsInUse.C"));
      }
      wcStandardCapacityService.insertWCStandardCapacity(wcStandardCapacity);
    } else {
      wcStandardCapacityService.updateWCStandardCapacity(wcStandardCapacity);
    }
    return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, null, null);
  }

  @RequestMapping("deleteWCStandardCapacity")
  @ResponseBody
  public DWZResult deleteMaterialType(Model model, String navTabId, HttpServletRequest request) {

    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    String[] codes = ids.split(",");
    int len = codes.length;
    BatchJobReport[] reports = new BatchJobReport[len];
    boolean success = true;
    for (int i = 0; i < len; i++) {
      reports[i] = wcStandardCapacityService.deleteWCStandardCapacity(codes[i], getSiteCode());
      if (!reports[i].isSuccess()) {
        success = false;
      }
    }
    if (success) {
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }
  }

  @RequestMapping("selectUnusedWorkCenter")
  @ResponseBody
  public List<WorkCenter> selectUnusedWorkCenterList(String workCenterCode) {
    return wcStandardCapacityService.selectUnusedWorkCenterList(workCenterCode, getSiteCode());
  }

  @RequestMapping("checkUnusedWorkCenter")
  @ResponseBody
  public boolean checkUnusedWorkCenter(String workCenterCode) {
    WorkCenter workCenter = wcStandardCapacityService.getUnusedWorkCenter(workCenterCode, getSiteCode());
    return workCenter != null && workCenter.getLockFlag() != 1;
  }

}
